/**
 * 全局参数
 */

var formName,rule,minLength,maxLength,choiceList=[],required;
window.onload=function(){
    setType();
    setConfig();
    setRule();
    setLength();
    

    $('#choiceInput').onkeyup=function(e) {
        if (e.keyCode == 188 || e.keyCode == 32 || e.keyCode == '13') {
            setChoice();
        }

    };
    $('#create').onclick=function(){
        
        var form1=new form(formName,rule,minLength,maxLength,choiceList,required);
        console.log('form is created ');
        $('#submit').style.display='block'
       
    }
    

}

/**
 * 设置表单类型
 */
function setType(){
    //给表单类型选择框设置单选功能
    var typeList=$a('#type>input');
    formName="输入框";
    showField();

    for(var i=0;i<typeList.length;i++){
        typeList[i].index=i;
        addEvent(typeList[i],'change',function(){
            getFormName(this.name);
            showField();
            $('#formName').value=formName;
            for(var m=0;m<typeList.length;m++){
                if(m!=this.index){
                    typeList[m].checked=false;
                }
            }
        })
    }
    
}
/**
 * 设定配置
 */
function setConfig(){
    formName=$('#formName').value;
    required=$('#formNecessary').checked;
    addEvent($('#formNecessary'),'change',function(){
        required=$('#formNecessary').checked;
        console.log('required changed to ' +required)
    })

}
/**
 * 设定规则
 */
function setRule(){
    
    var ruleList=$a('#rule>input');
    rule='text'
    for(var i=0;i<ruleList.length;i++){
        ruleList[i].index=i;
        addEvent(ruleList[i],'change',function(){
            for(var m=0;m<ruleList.length;m++){
                if(m!=this.index){
                   
                    ruleList[m].checked=false;
                    
                }
            
            }
            rule=this.name;
            
            
        })
    }

}

function setLength(){
    minLength=$('#minLength').value;
    maxLength=$('#maxLength').value;
    
    
    addEvent($('#minLength'),'keyup',function(){
        minLength=$('#minLength').value;
        if(minLength<4){
            $('#err').innerHTML='最小长度不得小于4';
            minLength=4;
            err(this);
        }else{
            $('#err').innerHTML='';
            minLength=$('#minLength').value;err(this,1);
        }
        console.log(minLength);
    });
    addEvent($('#maxLength'),'keyup',function(){
        maxLength=$('#maxLength').value;
        if(maxLength>20){
            $('#err').innerHTML='最大长度不得大于20';err(this);
            maxLength=20;
        }else if(maxLength<minLength){
            $('#err').innerHTML='最大长度不得小于最小长度';err(this);
            maxLength=minLength;
        }else{
            $('#err').innerHTML='';
            maxLength=$('#maxLength').value;err(this,1);
        }
         

        console.log(maxLength);
    })
}


function setChoice(){
    var choiceSum=$('#choiceInput').value;
    var splitReg=new RegExp('[,。， ./\n\t]')
    var cList=choiceSum.split(splitReg);
    choiceList=choiceList.concat(cList)

    var choiceTag=$('#choiceTag')
    choiceTag.innerHTML="";

    sumList(choiceList);
    
    for(let item in choiceList){

        var delSpan=document.createElement('span');
        delSpan.innerHTML="删除 ";
        delSpan.className="hidden"
        var itemSpan=document.createElement('span');
        itemSpan.innerHTML=choiceList[item];
        var choiceSpan=document.createElement('span');

        choiceSpan.className="choiceSpan";
        choiceSpan.appendChild(delSpan);
        choiceSpan.appendChild(itemSpan);
        choiceTag.appendChild(choiceSpan);
        
        addEvent(choiceSpan,'click',function(){
            choiceTag.removeChild(this);
            choiceList.splice(item,1);
        })
    };

    $('#choiceInput').value=""
}